package com.oracle.cmp.service;

import com.oracle.cmp.common.Constant;
import com.oracle.cmp.common.Result;
import com.oracle.cmp.entity.Parts;
import com.oracle.cmp.entity.PartsRepertory;
import com.oracle.cmp.entity.Partsrepbill;
import com.oracle.jdbc.util.Dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PartsRepetoryBillImpl implements PartsRepetoryBillService {
    @Override
    public Result save(Partsrepbill partsrepbill) {
        if (partsrepbill.getBillcount()==null){
            return new Result(false,null,"空值");
        }
        String sql = "select PartsId,PartsReqCount from biz_partsrepertory where PartsId=?";
        List<PartsRepertory> partsRepertory = Dao.query(sql, PartsRepertory.class, partsrepbill.getPartsid());
        if (partsRepertory == null||partsRepertory.size()==0) {
            if ("in".equals(partsrepbill.getBillflag())){
                String sql1="insert into biz_partsrepertory values(default,?,?)";
                Dao.executeSql(sql1,partsrepbill.getPartsid(),partsrepbill.getBillcount());
            }else if ("out".equals(partsrepbill.getBillflag())){
                return new Result(false, null, "配件不存在");
            }

        }
        if ("out".equals(partsrepbill.getBillflag())){
            if (partsRepertory.get(0).getPartsreqcount()<partsrepbill.getBillcount()){
                return new Result(false,null,"此配件库存不足！");
            }
            String sql3="ALTER TABLE biz_partsrepbill auto_increment=1";
            Dao.executeSql(sql3);
            String sql1 = "insert into biz_partsrepbill values(default,?,?,?,?,?,?)";
            Dao.executeSql(sql1,partsrepbill.getBillflag(),partsrepbill.getBilltype(),partsrepbill.getPartsid(),partsrepbill.getBillcount(),partsrepbill.getBilltime(),partsrepbill.getBilluser());
            String sql2 = "update biz_partsrepertory set PartsReqCount=? where PartsId=?";
            Dao.executeSql(sql2, partsRepertory.get(0).getPartsreqcount() - partsrepbill.getBillcount(), partsrepbill.getPartsid());
            return new Result(true, null, "出库成功");
        }
        if ("in".equals(partsrepbill.getBillflag())){
            String sql3="ALTER TABLE biz_partsrepbill auto_increment=1";
            Dao.executeSql(sql3);
            String sql1 = "insert into biz_partsrepbill values(default,?,?,?,?,?,?)";
            Dao.executeSql(sql1,partsrepbill.getBillflag(),partsrepbill.getBilltype(),partsrepbill.getPartsid(),partsrepbill.getBillcount(),partsrepbill.getBilltime(),partsrepbill.getBilluser());
            String sql2 = "update biz_partsrepertory set PartsReqCount=? where PartsId=?";
            Dao.executeSql(sql2, partsRepertory.get(0).getPartsreqcount() + partsrepbill.getBillcount(), partsrepbill.getPartsid());
            return new Result(true, null, "入库成功");
        }
        return  new Result(false,null,"操作失败！");

    }

    @Override
    public Result queryForPage(Map<String, Object> map, int pageNum) {
        String sql="SELECT billid,m.NAME as billflag,sc.NAME as billtype,b.PartsName,billcount,billtime,su.LoginName AS billuser FROM biz_partsrepbill zp LEFT OUTER JOIN sys_code sc on zp.billtype=sc.code\n" +
                " LEFT OUTER JOIN biz_parts b on zp.partsid=b.partsId LEFT OUTER JOIN sys_user su on zp.billuser=su.UserId LEFT OUTER JOIN (SELECT DISTINCT CODE,NAME FROM biz_partsrepbill b,sys_code sc WHERE b.billflag=sc.code)as m on zp.billflag=m.code\n" +
                " WHERE 1=1";
        List params = new ArrayList();
        if(map.get("partsName")!=null&&!"".equals(map.get("partsName"))){
            sql += " and PartsName=?";
            params.add(map.get("partsName"));
        }
        if(map.get("billflag")!=null&&!"".equals(map.get("billflag"))){
            sql += " and billflag=?";
            params.add(map.get("billflag"));
        }
        if(map.get("billtype")!=null&&!"".equals(map.get("billtype"))){
            sql += " and billtype=?";
            params.add(map.get("billtype"));
        }
        if(map.get("billtime")!=null&&!"".equals(map.get("billtime"))){
            sql += " and billtime=?";
            params.add(map.get("billtime"));
        }
        sql+=" limit ?,?";
        params.add((pageNum-1)* Constant.PAGESIZE);
        params.add(Constant.PAGESIZE);
        List<Partsrepbill> list = Dao.query(sql,Partsrepbill.class,params.toArray());
        return new Result(true,list,"查询成功");
    }

    @Override
    public int count(Map<String, Object> map) {
        String sql="SELECT count(0) FROM(SELECT billid,m.NAME as billflag,sc.NAME as billtype,b.PartsName,billcount,billtime,su.LoginName AS billuser  FROM biz_partsrepbill zp LEFT OUTER JOIN sys_code sc on zp.billtype=sc.code" +
                " LEFT OUTER JOIN biz_parts b on zp.partsid=b.partsId" +
                " LEFT OUTER JOIN sys_user su on zp.billuser=su.UserId" +
                " LEFT OUTER JOIN (SELECT DISTINCT CODE,NAME FROM biz_partsrepbill b,sys_code sc WHERE b.billflag=sc.code)as m ON zp.billflag=m.code" +
                " WHERE 1=1";

        List params = new ArrayList();
        if(map.get("partsName")!=null&&!"".equals(map.get("partsName"))){
            sql += " and PartsName=?";
            params.add(map.get("partsName"));
        }
        if(map.get("billflag")!=null&&!"".equals(map.get("billflag"))){
            sql += " and billflag=?";
            params.add(map.get("billflag"));
        }
        if(map.get("billtype")!=null&&!"".equals(map.get("billtype"))){
            sql += " and billtype=?";
            params.add(map.get("billtype"));
        }
        if(map.get("billtime")!=null&&!"".equals(map.get("billtime"))){
            sql += " and billtime = ?";
            params.add(map.get("billtime"));
        }
        sql+=")as n";
        int total = ((Long)Dao.queryOne(sql,params.toArray())[0]).intValue();
        return total;
    }
}
